package fr.xebia.view;

import java.util.LinkedList;
import java.util.List;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

import fr.xebia.data.Adresse;
import fr.xebia.service.AdresseService;

/**
 * 
 */
public class AdresseAction extends GenericAction implements ModelDriven<Adresse> {

	private static final long serialVersionUID = 1L;

	private AdresseService adresseService;

	private Adresse adresse;

	public Adresse getAdresse() {
		return adresse;
	}

	/**
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */
	public Adresse getModel() {
		adresse = new Adresse();
		return adresse;
	}

	/**
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		super.prepare();
		logger.debug("Preparing " + getClass().getName());
		adresseService = serviceLocator.getAdresseService();
	}

	public String prepareadd() throws Exception {
		return Action.SUCCESS;
	}

	public String list() throws Exception {
		try {
			List<Adresse> adresses = adresseService.list();
			request.setAttribute("adresses", adresses);
			logger.debug("Found " + adresses.size() + " adresses");
			return Action.SUCCESS;
		} catch (Exception e) {
			request.setAttribute("adresses", new LinkedList<Adresse>());
			return handleError("Can't get adresses", e);
		}
	}

	public String search() throws Exception {
		try {
			List<Adresse> adresses = adresseService.search(adresse);
			request.setAttribute("adresses", adresses);
			logger.debug("Found " + adresses.size() + " adresses");
			return Action.SUCCESS;
		} catch (Exception e) {
			request.setAttribute("adresses", new LinkedList<Adresse>());
			return handleError("Can't get adresses", e);
		}
	}

	public String add() throws Exception {
		try {
			logger.info("Adding new adresse");
			Long id = (Long) adresseService.save(adresse);
			adresse.setId(id);
			addActionMessage("Added new adresse with id: " + adresse.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't add adresse", e);
		}
	}

	public String update() throws Exception {
		try {
			logger.info("Updating adresse with id: " + adresse.getId());
			adresseService.update(adresse);
			addActionMessage("Updated adresse with id: " + adresse.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't save adresse", e);
		}
	}

	public String edit() throws Exception {
		try {
			logger.info("Getting adresse with id: " + adresse.getId());
			adresse = adresseService.get(adresse.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't get adresse", e);
		}
	}

	public String delete() throws Exception {
		try {
			logger.info("Deleting adresse with id: " + adresse.getId());
			adresseService.deleteById(adresse.getId());
			addActionMessage("Deleted adresse with id: " + adresse.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't delete adresse", e);
		}
	}
}
